package 链表;

public class Solution19 {

    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2, node1);
        ListNode node3 = new ListNode(6, node2);
        ListNode node4 = new ListNode(3, node3);
        ListNode node5 = new ListNode(2, node4);
        ListNode node = removeNthFromEnd(node2, 2);
        System.out.println(node);
    }

    public static ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode slow = new ListNode(0);
        ListNode fast = new ListNode(0);
        slow.next = head;
        fast.next = head;

        for (int i = 0; i <= n; i++) {
            fast = fast.next;
        }
        if (fast==null){
            return head.next;
        }

        while (fast != null) {
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return head;






//        ListNode slow = new ListNode(0);
//        ListNode fast = new ListNode(0);
//        slow.next = head;
//        fast.next = head;
//        for (int i = 0; i <= n; i++) {
//            fast = fast.next;
//        }
//        if (fast == null) {
//            return head.next;
//        }
//        while (fast != null) {
//            slow = slow.next;
//            fast = fast.next;
//        }
//        slow.next = slow.next.next;
//        return head;
    }
}
